package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Class responsible for displaying a list of tickets
 * 
 * @author Amigos do Noddy
 *
 */
public class TicketsView extends Composite {
	
	/**
	 * Constructor
	 */
	public TicketsView() {
		
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		horizontalPanel_1.add(verticalPanel_1);
		verticalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Button btnViewPend = new Button();
		horizontalPanel_2.add(btnViewPend);
		btnViewPend.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) 
			{
				Default.loadComposite(new TicketsViewPending());
			}
		});
		btnViewPend.setStyleName("button");
		btnViewPend.setText("Ver pendentes");
		//System.out.println("View Tickets");
		//Integer type = new Integer(Default.getSession("Tipo").toString());
		
		ServiceTickets.Util.getInstance().getAssisTickets(new Integer(Default.getSession("IDPerson").toString()),new ViewTicketsAsyncCallback(verticalPanel_1));
	}
	
	/**
	 * AsyncCallback responsible for receiving list of tickets and displaying them
	 * 
	 * @author Amigos do Noddy
	 *
	 */
	static class ViewTicketsAsyncCallback implements AsyncCallback {
		  VerticalPanel vp;
		  //private Integer personLvl;
		  public ViewTicketsAsyncCallback(VerticalPanel vp) {
			  this.vp= vp;
			  //this.personLvl = lvl;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  ArrayList Tickets= (ArrayList)object;
			  ArrayList idTickets = (ArrayList)Tickets.get(0);
			  ArrayList titles = (ArrayList)Tickets.get(1);
			  ArrayList status = (ArrayList)Tickets.get(2);
			  
			  Grid grid = new Grid(); 
			  
			  System.out.println("Criou a grid");
			  grid.resize(idTickets.size()+1, 5);
			  grid.setStyleName("grid");
			  grid.setBorderWidth(1);
			  grid.setCellSpacing(4);
			  			  
			  Label lbl1 = new Label();
			  lbl1.setText("#");
			  lbl1.setStyleName("section_title");
			  grid.setWidget(0, 0, lbl1);
			  
			  Label lbl2 = new Label();
			  lbl2.setText("Título");
			  lbl2.setStyleName("section_title");
			  grid.setWidget(0, 1, lbl2);
			  		  
			  Label lbl3 = new Label();
			  lbl3.setText("Estado");
			  lbl3.setStyleName("section_title");
			  grid.setWidget(0, 2, lbl3);
			  
			  Label lbl4 = new Label();
			  lbl4.setText("");
			  lbl4.setStyleName("section_title");
			  grid.setWidget(0, 3, lbl4);
			 
			  Label lbl5 = new Label();
			  lbl5.setText("");
			  lbl5.setStyleName("section_title");
			  grid.setWidget(0, 4, lbl5);
			  		  
			  for(int i = 0; i < idTickets.size(); i++)
			  {		  
				  Label lblCount = new Label();
				  lblCount.setText(""+(i+1));
				  lblCount.setStyleName("label");
				  grid.setWidget(i+1, 0, lblCount);
				  
				  Label lblTitle = new Label();
				  lblTitle.setText(titles.get(i).toString());
				  lblTitle.setStyleName("label");
				  grid.setWidget(i+1, 1, lblTitle);
				  
				  Label lblStatus = new Label();
				  if(status.get(i).toString().equals("1"))
				  {
					  lblStatus.setText("Pendente");
				  }
				  else if(status.get(i).toString().equals("2"))
				  {
					  lblStatus.setText("A resolver");
				  }
				  else
				  {
					  lblStatus.setText("Resolvido");
				  }
				  lblStatus.setStyleName("label");
				  grid.setWidget(i+1, 2, lblStatus);
				  
				  		  			  
				  Button btn = new Button();
				  btn.setText("Ver");
				  btn.setStyleName("button");
				  btn.addClickListener(new TicketViewOnClick((Integer)idTickets.get(i)));
				  grid.setWidget(i+1, 3, btn);
				  
				  Button btnRemove = new Button("Remover");
				  btnRemove.setStyleName("button");
				  btnRemove.addClickListener(new TicketRemoveOnClick((Integer)idTickets.get(i)));
				  grid.setWidget(i+1, 4, btnRemove);
				  
			  }
			  
			  vp.add(grid);
			  
			  /* Versão sem Grids
			  String presentStatus;
	    	  if(idTickets.size() > 0)
	    	  {
	    		  Integer idTick;	    		  
	    		  for(int i = 0; i < idTickets.size(); i++)
	    		  {
	    			  
	    			  HorizontalPanel hp = new HorizontalPanel();
	    		 	  hp.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
	    		 	  hp.setSpacing(7);
	    		 	  
	    			  Label lblTitle = new Label();
	    			  lblTitle.setText((String)titles.get(i));
	    			  lblTitle.setStyleName("label");
	    			  
	    			  Label lblStatus = new Label();
	    			  lblStatus.setStyleName("label");
	    			  
	    			  presentStatus = new String((String)status.get(i));
	    			  
	    			  if(presentStatus.equals("1"))
	    			  {
	    				  lblStatus.setText("Pendente");
	    			  }
	    			  else if(presentStatus.equals("2"))
	    			  {
	    				  lblStatus.setText("A resolver");
	    			  }
	    			  else
	    			  {
	    				  lblStatus.setText("Fechado");
	    			  }
	    			      			  
	    			  
	    			  Button btnView = new Button("Ver");
	    			  btnView.setStyleName("button");
	    			  
	    			  idTick = (Integer)idTickets.get(i);
	    			  btnView.addClickListener(new TicketViewOnClick(idTick));
	    			  
	    			  hp.add(lblTitle);
	    			  hp.add(lblStatus);
	    			  hp.add(btnView);
	    			  //if(personLvl.intValue() > 1)
	    			  //{
	    				  Button btnRemove = new Button("Remover");
	    				  btnRemove.setStyleName("button");
	    				  btnRemove.addClickListener(new TicketRemoveOnClick(idTick));
	    				  hp.add(btnRemove);
	    			  //}
	    			  
	    			  vp.add(hp);	    			  
	    		  }
	    		  
	    	  } 
	    	  */  	
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		  
		}

}
